#include "stdafx.h"
#include <Tlhelp32.h>

BOOL SetProcessList(HWND hWndCombox)
{
	DWORD dwIndex = 0;
	HANDLE hSnapShot = NULL;
	PROCESSENTRY32 pe32;
	TCHAR szItemString[MAX_PATH];

	pe32.dwSize = sizeof(PROCESSENTRY32);
	hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapShot == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}
	
	SendMessage(hWndCombox, CB_RESETCONTENT, 0, 0);
	Process32First(hSnapShot, &pe32);
	do
	{
		ZeroMemory(szItemString, MAX_PATH*sizeof(TCHAR));
		if (pe32.th32ProcessID == 0 || pe32.th32ProcessID == GetCurrentProcessId())
		{
			continue;
		}
		_stprintf_s(szItemString, MAX_PATH, _T("%s:[%04d]"), pe32.szExeFile, pe32.th32ProcessID);
		dwIndex = SendMessage(hWndCombox, CB_ADDSTRING, 0, (LPARAM)szItemString);
		SendMessage(hWndCombox, CB_SETITEMDATA, dwIndex, pe32.th32ProcessID);

	}while(Process32Next(hSnapShot, &pe32));
	SendMessage(hWndCombox, CB_SETCURSEL, 0, 0);

	//CloseHandle(hSnapShot);
	return TRUE;
}

BOOL SetModuleList(HWND hWndList, DWORD dwPID)
{
	DWORD dwIndex = 0;
	HANDLE hSnapShot = NULL;
	MODULEENTRY32 me32;

	LVITEM lvItem;
	ZeroMemory(&lvItem, sizeof(lvItem));

	me32.dwSize = sizeof(MODULEENTRY32);
	SendMessage(hWndList, LVM_DELETEALLITEMS, 0, 0);
	hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
	if (hSnapShot == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	Module32First(hSnapShot, &me32);
	do
	{
		lvItem.mask = LVIF_TEXT|LVIF_PARAM;
		lvItem.iItem = 0;
		lvItem.iSubItem = 0;
		lvItem.pszText = me32.szModule;
		lvItem.lParam = (LPARAM)me32.hModule;
		dwIndex = SendMessage(hWndList, LVM_INSERTITEM, 0, (LPARAM)&lvItem);

		lvItem.mask = LVIF_TEXT;
		lvItem.iItem = dwIndex;
		lvItem.iSubItem = 1;
		lvItem.pszText = me32.szExePath;
		SendMessage(hWndList, LVM_SETITEM, 0, (LPARAM)&lvItem);
	}while(Module32Next(hSnapShot, &me32));
	SendMessage(hWndList, CB_SETCURSEL, 0, 0);

	CloseHandle(hSnapShot);
	return TRUE;
}
